﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Marmat.Forms.Design
{
    public partial class DataColumnForm : Form
    {

        private readonly Marmat.Forms.Local.DataGrid _dataGrid;
        private readonly IList<Marmat.Forms.Views.DataGrid.Column> _columns;
        public DataColumnForm(Marmat.Forms.Local.DataGrid dataGrid, IList<Marmat.Forms.Views.DataGrid.Column> columns)
        {
            InitializeComponent();

            this._dataGrid = dataGrid;
            this._columns = columns;
            DevExpress.XtraGrid.Views.Grid.GridView gridView = this.BGrid1.GetMainView<DevExpress.XtraGrid.Views.Grid.GridView>();
            gridView.OptionsFind.AlwaysVisible = true;
        }

        private void DataColumnForm_Load(object sender, EventArgs e)
        {
            Marmat.Forms.Design.DesignSupport.InitializeService(this._dataGrid);
            Marmat.Forms.IDesignSupport design = null;
            var service = design.GetDataGridService();
            if (service != null)
            {
                try
                {
                    string connectionName = this._dataGrid.ConnectionName;
                    if (!Marmat.Data.Connections.Contains(connectionName))
                        throw new NullReferenceException($"数据源 {connectionName} 不存在。");
                    string loadTableName = this._dataGrid.LoadTableName;
                    ColumnDescription[] columns = Marmat.Forms.AsyncHelper.Run(() => service.GetColumnDescriptionCollection(connectionName, loadTableName));
                    foreach (var column in columns)
                    {
                        var row = this.BGrid1.AddNew();
                        row["选择"] = this._columns.FirstOrDefault(x => x.Name == column.Name) != null;
                        row["列名"] = column.Name;
                        row["数据类型"] = column.DataType;
                        row["可否为空"] = !column.IsRequired;
                        row["是否主键"] = column.IsPrimaryKey;
                        row["自动增长"] = column.IsIdentity;
                        //row["占用字节"] = 32;
                        row["长度"] = column.MaxLength;
                        //row["小数位数"] = 0;
                        //row["默认值"] = column.def
                        row["字段描述"] = column.Caption;
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }

                this.BGrid1.AcceptChanges();
            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            foreach (Marmat.Forms.Views.DataGrid.Row row in this.BGrid1.Rows)
            {
                row["选择"] = true;
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            foreach (Marmat.Forms.Views.DataGrid.Row row in this.BGrid1.Rows)
            {
                row["选择"] = !(bool)row["选择"];
            }
        }

        private void BGrid1_ShowingEditor(object sender, Views.DataGrid.RowColCancelEventArgs e)
        {
            if (e.Column.Name != "选择")
            {
                e.Cancel = true;
            }
        }
    }
}
